package com.insight.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.hibernate.annotations.CreationTimestamp;

import java.time.LocalDateTime;

/**
 * T118: 操作日志实体类
 * 记录系统所有API操作的审计日志
 *
 * @author Insight Team
 */
@Entity
@Table(name = "operation_log",
        indexes = {
                @Index(name = "idx_operation_type", columnList = "operation_type"),
                @Index(name = "idx_operation_module", columnList = "operation_module"),
                @Index(name = "idx_operation_time", columnList = "operation_time"),
                @Index(name = "idx_operator_ip", columnList = "operator_ip"),
                @Index(name = "idx_success", columnList = "success"),
                @Index(name = "idx_resource", columnList = "resource_type, resource_id")
        })
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class OperationLog {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 操作类型 (CREATE, UPDATE, DELETE, QUERY, EXPORT等)
     */
    @Column(name = "operation_type", nullable = false, length = 50)
    private String operationType;

    /**
     * 操作模块 (DATASOURCE, METADATA, EXPORT, RELATIONSHIP等)
     */
    @Column(name = "operation_module", nullable = false, length = 50)
    private String operationModule;

    /**
     * 操作描述
     */
    @Column(name = "operation_description", columnDefinition = "TEXT")
    private String operationDescription;

    /**
     * HTTP请求方法 (GET, POST, PUT, DELETE等)
     */
    @Column(name = "request_method", length = 10)
    private String requestMethod;

    /**
     * 请求URL
     */
    @Column(name = "request_url", length = 500)
    private String requestUrl;

    /**
     * 请求参数 (JSON格式)
     */
    @Column(name = "request_params", columnDefinition = "TEXT")
    private String requestParams;

    /**
     * 响应结果 (JSON格式)
     */
    @Column(name = "response_result", columnDefinition = "TEXT")
    private String responseResult;

    /**
     * HTTP响应状态码
     */
    @Column(name = "response_status")
    private Integer responseStatus;

    /**
     * 操作者IP地址
     */
    @Column(name = "operator_ip", length = 50)
    private String operatorIp;

    /**
     * 操作者User-Agent
     */
    @Column(name = "operator_user_agent", length = 500)
    private String operatorUserAgent;

    /**
     * 操作时间
     */
    @CreationTimestamp
    @Column(name = "operation_time", nullable = false, updatable = false)
    private LocalDateTime operationTime;

    /**
     * 执行耗时(毫秒)
     */
    @Column(name = "execution_time")
    private Long executionTime;

    /**
     * 操作是否成功
     */
    @Column(name = "success", nullable = false)
    @Builder.Default
    private boolean success = true;

    /**
     * 错误消息
     */
    @Column(name = "error_message", columnDefinition = "TEXT")
    private String errorMessage;

    /**
     * 错误堆栈跟踪
     */
    @Column(name = "error_stack_trace", columnDefinition = "TEXT")
    private String errorStackTrace;

    /**
     * 资源类型 (DataSource, MetadataTable, DictionaryExport等)
     */
    @Column(name = "resource_type", length = 50)
    private String resourceType;

    /**
     * 资源ID
     */
    @Column(name = "resource_id")
    private Long resourceId;
}
